import { parse } from 'crypto-js/enc-utf8';
import { encrypt, decrypt } from 'crypto-js/aes';
import pkcs7 from 'crypto-js/pad-pkcs7';
import ECB from 'crypto-js/mode-ecb';
import UTF8 from 'crypto-js/enc-utf8';


interface EncryptionParams {
    key: string
    iv: string
}

type WordArray = CryptoJS.lib.WordArray;

export default class Encryption {
    private key: WordArray;
    private iv: WordArray;

    constructor(opt: EncryptionParams) {
        const { key, iv } = opt
        this.key = parse(key)
        this.iv = parse(iv)
    }

    get getOptions(){
        return {
            mode:ECB,
            padding:pkcs7,
            iv:this.iv
        }
    }

    encrypt(val:string){
        return encrypt(val,this.key,this.getOptions).toString()

    }
    decrypt(val:string){
        return decrypt(val,this.key,this.getOptions).toString(UTF8)

    }
}